package startup;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;

import org.dom4j.DocumentException;


import indi.zjh.dp.access.HttpServer;
import indi.zjh.dp.db.Configuration;
import indi.zjh.dp.db.DataSource;
import indi.zjh.dp.db.Session;
import indi.zjh.dp.error.MySQLConnectionUseupException;
import indi.zjh.dp.orm.Query;
import indi.zjh.dp.orm.Result;
import indi.zjh.dp.util.RedisUtil;
import indi.zjh.dp.vc.Commit;
import indi.zjh.dp.vc.Version;
import log.Label;
import log.Log;


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try
		{
			Configuration.getInstance().setVersion(new Version());
			Configuration.getInstance().getVersion().analyzeClassRelation();
			File file = new File("first");
			if(!file.exists())
			{
				file.createNewFile();
				Configuration.getInstance().getVersion().clear();
				Configuration.getInstance().getVersion().initial();
			}
			
			Commit commit = new Commit();
			commit.submit("日志系统:"+new Date().getTime());
			
			// 初始化缓存
			RedisUtil.init();
		
			// 初始化
			init();


			HttpServer server = new HttpServer();
			server.start(8080);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private static void init() throws SQLException, MySQLConnectionUseupException, DocumentException, Exception
	{
		// 忽略写入数据源的类
		DataSource.ignore(Log.class);
		DataSource.ignore(Label.class);
		
		Session session = Configuration.getSessionFactory().createSession();
		try
		{

			session.commit();
		}
		catch(Exception e)
		{
			session.rollback();
			e.printStackTrace();
		}
		finally {
			try {
				session.close();
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
